package com.kyzh.core.adapters

import android.view.View
import android.widget.Button
import android.widget.RatingBar
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.atools.ui.ArcButton
import com.gushenge.atools.util.AView
import com.gushenge.core.beans.Collection
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.impls.UserImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.kyzh.core.activities.BaseFragmentActivity
import com.kyzh.core.utils.dip
import com.kyzh.core.utils.startActivity
import com.kyzh.core.utils.startGameDetailActivity
import com.gushenge.core.toast


class CollectionAdapter(val beans:ArrayList<Collection>):BaseMultiItemQuickAdapter<Collection, BaseViewHolder>(beans) {
    init {
        addItemType(0, R.layout.frag_home_game_item)
        addItemType(2, R.layout.collection_deal_item)
        addItemType(1, R.layout.collection_product_item)
    }
    override fun convert(helper: BaseViewHolder, item: Collection) {
        item.let {
            when(helper.itemViewType){
                0->{
                    val root = helper.getView<RelativeLayout>(R.id.root)
                    val bq = helper.getView<RecyclerView>(R.id.bq)
                    val desc = helper.getView<TextView>(R.id.desc)
                    val start = helper.getView<Button>(R.id.start)
                    val discount = helper.getView<ArcButton>(R.id.btDiscount)
                    val rbStar = helper.getView<RatingBar>(R.id.rbStar)
                    rbStar.rating = item.point.toFloat()
                    helper.setText(R.id.name,it.name)
                            .setText(R.id.desc,it.summary)
                            .setBackgroundResource(R.id.start,R.drawable.button_bg)
                    start.setOnClickListener {
                        collectCancel(item.id,helper.itemViewType,helper.adapterPosition)
                    }
                    AView.setWidth(start,context.dip(80))
                    start.text = context.getString(R.string.collectCancel)
                    Glide.with(context).load(it.icon).into(helper.getView(R.id.image))
                    root.setOnClickListener { context.startGameDetailActivity(item.gid) }
                    if (item.biaoqian.isNotEmpty()) {
                        bq.visibility = View.VISIBLE
                        desc.visibility = View.GONE
                        bq.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                        bq.adapter = BiaoqianAdapter( item.biaoqian)
                    }else{
                        bq.visibility = View.GONE
                        desc.visibility = View.VISIBLE
                    }

                    discount.visibility = View.GONE
                    if(it.system_type==1){
                        helper.setText(R.id.type,"${item.size}M | ${item.type} ")
                    }else{
                        helper.setText(R.id.type,"${item.type}")
                    }
                }
                1->{
                    helper.setText(R.id.tvName, it.name)
                        .setText(R.id.tvPoint, String.format(context.getString(R.string.point),it.money)  )
                    Glide.with(context).load(it.icon).into(helper.getView(R.id.ivIcon))
                    val start = helper.getView<Button>(R.id.btGet)
                    start.setOnClickListener {
                        collectCancel(item.id, helper.itemViewType, helper.adapterPosition)
                    }
                }
                2 -> {
                    helper.setText(R.id.time,"${context.getString(R.string.addedTime)}: ${it.time}")
                            .setText(R.id.name,it.name)
                            .setText(R.id.content,it.introduction)
                            .setText(R.id.price,"¥${it.money}")
                    Glide.with(context).load(it.icon).into(helper.getView(R.id.image))
                    val start = helper.getView<Button>(R.id.start)
                    start.setOnClickListener {
                        collectCancel(item.id,helper.itemViewType,helper.adapterPosition)
                    }
                    helper.itemView.setOnClickListener {
                        context.startActivity<BaseFragmentActivity>(
                                GlobalKeys.TYPE to BaseFragmentActivity.DEAL_PRODUCT_DETAIL,
                                GlobalKeys.KEY to beans[helper.adapterPosition].goods_id
                        )
                    }
                }
                else->{}
            }
        }
    }


    private fun collectCancel(id:String,type:Int,position:Int){
        UserImpl.collectCancel(id,type,object : ResultListener {
            override fun success() {
                context.getString(R.string.cancelSuccess).toast()
                beans.removeAt(position)
                notifyDataSetChanged()
            }

            override fun error(error: String) {
                error.toast()
            }
        })
    }
}